#include<iostream>
#include<vector>
using namespace std;

class Solution {
    int aim;
    vector<int> path;
    vector<vector<int>> ret;
public:
    vector<vector<int>> combinationSum(vector<int>& nums, int target) {
        aim = target;
        dfs(nums, 0, 0);
        return ret;

    }

    void dfs(vector<int>& nums, int pos, int sum)
    {
        if (sum == aim)
        {
            ret.push_back(path);
            return;
        }

        if (sum > aim || pos == nums.size())
        {
            return;
        }

        for (int i = pos; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            dfs(nums, i, sum + nums[i]);
            path.pop_back();
        }
    }
};